import * as TemplatesService from '../../services/Editor/templates';
import persistHelper from '../../utils/persistHelper';

export default {
    namespace: 'templates',
    state: [],
    reducers: {
        save(prevState, { payload: state }) {
            return [...state];
        },
    },
    effects: {
        *queryTemplates({ payload }, { put, call }) {
            const { data } = yield call(TemplatesService.fetch);
            yield put({
                type: 'save',
                payload: data.resource_list,
            });
        },
    },
    subscriptions: {
        setup({ dispatch }) {
            persistHelper.ready(() => dispatch({ type: 'queryTemplates' }), 'templates');
        },
    },
};
